package com.hiroshi.cimoc.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import java.util.HashSet;

public class NetUtils {

    public static final int NETWORN_NONE = 0;
    //wifi连接
    public static final int NETWORN_WIFI = 1;
    //手机网络数据连接类型
    public static final int NETWORN_2G = 2;
    public static final int NETWORN_3G = 3;
    public static final int NETWORN_4G = 4;
    public static final int NETWORN_5G = 5;
    public static final int NETWORN_MOBILE = 6;
    //2g网取值范围
    public static HashSet<Integer> twoGNet = new HashSet<>();
    //3g网取值范围
    public static HashSet<Integer> threeGNet = new HashSet<>();
    //4g网取值范围
    public static HashSet<Integer> fourGNet = new HashSet<>();
    //5g网取值范围
    public static HashSet<Integer> fiveGNet = new HashSet<>();

    static {
        twoGNet.add(TelephonyManager.NETWORK_TYPE_GPRS); // 联通2g
        twoGNet.add(TelephonyManager.NETWORK_TYPE_CDMA); // 电信2g
        twoGNet.add(TelephonyManager.NETWORK_TYPE_EDGE); // 移动2g
        twoGNet.add(TelephonyManager.NETWORK_TYPE_1xRTT);
        twoGNet.add(TelephonyManager.NETWORK_TYPE_IDEN);

        threeGNet.add(TelephonyManager.NETWORK_TYPE_EVDO_A); // 电信3g
        threeGNet.add(TelephonyManager.NETWORK_TYPE_UMTS);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_EVDO_0);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_HSDPA);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_HSUPA);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_HSPA);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_EVDO_B);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_EHRPD);
        threeGNet.add(TelephonyManager.NETWORK_TYPE_HSPAP);

        fourGNet.add(TelephonyManager.NETWORK_TYPE_LTE);

        fiveGNet.add(TelephonyManager.NETWORK_TYPE_NR); //对应的20 只有依赖为android 10.0才有此属性
    }

    /**
     * 判断当前网络是否可用(6.0以上版本)
     * 实时
     *
     * @param context
     * @return
     */
    public static boolean isNetSystemUsable(Context context) {
        boolean isNetUsable = false;
        ConnectivityManager manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            NetworkCapabilities networkCapabilities =
                    manager.getNetworkCapabilities(manager.getActiveNetwork());
            isNetUsable = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
        }
        return isNetUsable;

    }

    /**
     * 获取当前网络连接类型
     *
     * @param context
     * @return
     */
    public static int getNetworkState(Context context) {
        //获取系统的网络服务
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //如果当前没有网络
        if (null == connManager){
            return NETWORN_NONE;
        }
        //获取当前网络类型，如果为空，返回无网络
        @SuppressLint("MissingPermission") NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return NETWORN_NONE;
        }
        // 判断是不是连接的是不是wifi
        @SuppressLint("MissingPermission") NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {
            NetworkInfo.State state = wifiInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORN_WIFI;
                }
            }
        }
        // 如果不是wifi，则判断当前连接的是运营商的哪种网络2g、3g、4g等
        @SuppressLint("MissingPermission") NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (null != networkInfo) {
            NetworkInfo.State state = networkInfo.getState();
            String strSubTypeName = networkInfo.getSubtypeName();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    int subtype = activeNetInfo.getSubtype();
                    //如果是2g类型
                    if (twoGNet.contains(subtype)) {
                        return NETWORN_2G;
                    }
                    //如果是3g类型
                    if (threeGNet.contains(subtype)) {
                        return NETWORN_3G;
                    }
                    //如果是4g类型
                    if (fourGNet.contains(subtype)) {
                        return NETWORN_4G;
                    }
                    if (fiveGNet.contains(subtype)) {
                        return NETWORN_5G;
                    }
                    if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                        return NETWORN_3G;
                    } else {
                        return NETWORN_MOBILE;
                    }
                }
            }
        }
        return NETWORN_NONE;
    }

    /**
     * 判断当前网络是否是流量
     * @param context
     * @return
     */
    public static boolean isDataTraffic(Context context){
        int res = getNetworkState(context);
        return res >= 2 && res <= 6;
    }
}
